package com.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dao.XuexijiluDao;
import com.entity.XuexijiluEntity;
import com.entity.view.XuexijiluView;
import com.entity.vo.XuexijiluVO;
import com.service.XuexijiluService;
import com.utils.PageUtils;
import com.utils.Query;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("xuexijiluService")
public class XuexijiluServiceImpl extends ServiceImpl<XuexijiluDao, XuexijiluEntity> implements XuexijiluService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<XuexijiluEntity> page = this.selectPage(
                new Query<XuexijiluEntity>(params).getPage(),
                new EntityWrapper<XuexijiluEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Wrapper<XuexijiluEntity> wrapper) {
        Page<XuexijiluView> page = new Query<XuexijiluView>(params).getPage();
        page.setRecords(baseMapper.selectListView(page, wrapper));
        PageUtils pageUtil = new PageUtils(page);
        return pageUtil;
    }

    @Override
    public List<XuexijiluVO> selectListVO(Wrapper<XuexijiluEntity> wrapper) {
        return baseMapper.selectListVO(wrapper);
    }

    @Override
    public XuexijiluVO selectVO(Wrapper<XuexijiluEntity> wrapper) {
        return baseMapper.selectVO(wrapper);
    }

    @Override
    public List<XuexijiluView> selectListView(Wrapper<XuexijiluEntity> wrapper) {
        return baseMapper.selectListView(wrapper);
    }

    @Override
    public XuexijiluView selectView(Wrapper<XuexijiluEntity> wrapper) {
        return baseMapper.selectView(wrapper);
    }

    @Override
    public List<Map<String, Object>> selectValue(Map<String, Object> params, Wrapper<XuexijiluEntity> wrapper) {
        return baseMapper.selectValue(params, wrapper);
    }

    @Override
    public List<Map<String, Object>> selectTimeStatValue(Map<String, Object> params, Wrapper<XuexijiluEntity> wrapper) {
        return baseMapper.selectTimeStatValue(params, wrapper);
    }

    @Override
    public List<Map<String, Object>> selectGroup(Map<String, Object> params, Wrapper<XuexijiluEntity> wrapper) {
        return baseMapper.selectGroup(params, wrapper);
    }

    @Override
    public PageUtils queryPage2(Map<String, Object> params, Wrapper sort) {
        Page<XuexijiluView> page = new Query<XuexijiluView>(params).getPage();
        page.setRecords(baseMapper.selectListView2(page, sort));
        PageUtils pageUtil = new PageUtils(page);
        return pageUtil;
    }

    @Override
    public void add(XuexijiluEntity xuexijilu) {
        String kechengmingcheng = xuexijilu.getKechengmingcheng();
        String xuehao = xuexijilu.getXuehao();
        BigDecimal xuexishizhang = xuexijilu.getXuexishizhang();
        BigDecimal divide = xuexishizhang.divide(BigDecimal.valueOf(60),2,BigDecimal.ROUND_HALF_UP);
        EntityWrapper<XuexijiluEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("kechengmingcheng", kechengmingcheng).and().eq("xuehao", xuehao);
        XuexijiluEntity result = this.selectOne(wrapper);
        if (result != null) {
            //    执行更新操作
            xuexijilu.setId(result.getId());
            xuexijilu.setXuexishizhang(divide.add(result.getXuexishizhang()));
            this.updateById(xuexijilu);
        } else {
            //    执行新增操作
            xuexijilu.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
            xuexijilu.setXuexishizhang(divide);
            this.insert(xuexijilu);
        }
    }


}
